#!/bin/bash

L4T_TOP=$(dirname $(pwd))
L4T_RK_PKG_DIR_NAME=$(basename $(pwd))
L4T_RK_PKG_ST_FILE=${L4T_TOP}/ritrontek_package_status.txt
L4T_KERNEL_IMG=resources/kernel/Image
L4T_ROOTFS_IMG=resources/bootloader/system.img
L4T_ROOTFS_RAW_IMG=resources/bootloader/system.img.raw

function check_root()
{
	if [[ $(id -u) -ne 0 ]]; then
		echo -e "\n[ERROR] Please run as root\n"
		exit 1
	fi
}

function check_folder()
{
	if [[ $(basename ${L4T_TOP}) != "Linux_for_Tegra" ]] \
		|| [[ ! $(basename $(pwd)) =~ ^Ritrontek_ ]]; then
		echo -e "\n[ERROR] This BSP should be extracted in the Linux_for_Tegra directory of your Jetpack installed."
		echo -e "          This install script must be run from the Linux_for_Tegra/Ritrontek_xxx directory."
		exit 2
	fi
}

function check_bsp_st()
{
	if [[ -f ${L4T_RK_PKG_ST_FILE} ]]; then
		echo -e "\n[ERROR] Not a clean BSP\n"
		exit 1
	fi
}

function set_bsp_st()
{
	echo "installed ${L4T_RK_PKG_DIR_NAME}" > ${L4T_RK_PKG_ST_FILE}
}

function install_bsp_files()
{
	local dir=$1
	local file
	if [[ ! -d ${dir} ]]; then
		echo -e "\n[ERROR] ${dir} doesnot exist."
		exit 3
	fi

	for file in $(ls ${dir})
	do
		current=${dir}/${file}
		if [[ -d ${current} ]]; then
			${FUNCNAME} ${current}
		else
			echo -e "[INFO] >>> ${current}"

			if [[ ${current} =~ ^.*\.tbz2$ ]]; then
				tar -jxf ${current} -C ${L4T_TOP}
			else
				local bsp_file=${L4T_TOP}/$(echo ${current} | cut -d/ -f2-)

				if [[ -f ${bsp_file} ]]; then
					echo -e "[INFO] backup : ${bsp_file}.bak"
					mv ${bsp_file} ${bsp_file}.bak
				fi
				echo -e "[INFO] install: ${bsp_file}"
				ln ${current} ${bsp_file}
			fi
		fi
	done
}

function get_kernel_vermagic()
{
	if [[ $# -le 0 ]]; then
		return
	fi

	local kernel_image_file=$1
	strings ${kernel_image_file} | grep -i "Linux version [0-9]" | awk '{print $3}'
}

function install_kernel_modules()
{
	local rootfs_img=$1
	local kernel_ver=$2

	local file src_file dst_file
	local mnt=/mnt
	declare -a files
	files=(
		"lib/modules/${kernel_ver}/kernel/drivers/hwmon/f75308.ko"
		"lib/modules/${kernel_ver}/kernel/drivers/hwmon/ina2xx.ko"
		"lib/modules/${kernel_ver}/kernel/drivers/hwmon/pwm-fan.ko"
		"lib/modules/${kernel_ver}/kernel/drivers/mtd/devices/qspi_mtd.ko"
		"lib/modules/${kernel_ver}/kernel/drivers/mtd/mtd_blkdevs.ko"
		"lib/modules/${kernel_ver}/kernel/drivers/mtd/mtdblock.ko"
		"lib/modules/${kernel_ver}/kernel/drivers/mtd/mtd.ko"
		"lib/modules/${kernel_ver}/kernel/drivers/net/ethernet/realtek/r8168.ko"
		"lib/modules/${kernel_ver}/kernel/drivers/spi/spi-tegra210-qspi.ko"
		"lib/modules/${kernel_ver}/modules.alias"
		"lib/modules/${kernel_ver}/modules.alias.bin"
		"lib/modules/${kernel_ver}/modules.builtin"
		"lib/modules/${kernel_ver}/modules.builtin.alias.bin"
		"lib/modules/${kernel_ver}/modules.builtin.bin"
		"lib/modules/${kernel_ver}/modules.builtin.modinfo"
		"lib/modules/${kernel_ver}/modules.dep"
		"lib/modules/${kernel_ver}/modules.dep.bin"
		"lib/modules/${kernel_ver}/modules.devname"
		"lib/modules/${kernel_ver}/modules.order"
		"lib/modules/${kernel_ver}/modules.softdep"
		"lib/modules/${kernel_ver}/modules.softdep"
		"lib/modules/${kernel_ver}/modules.symbols"
		"lib/modules/${kernel_ver}/modules.symbols.bin"
	)

	mount ${rootfs_img} ${mnt}

	echo -e "[INFO] install kernel modules from ${rootfs_img}:"
	for file in ${files[@]}; do
		src_file=${mnt}/${file}
		dst_file=${L4T_TOP}/rootfs/${file}

		if [[ -f ${dst_file} ]]; then
			continue
		fi

		if [[ -f ${src_file} ]]; then
			if [[ ! -d $(dirname ${dst_file}) ]]; then
				mkdir -p $(dirname ${dst_file})
			fi

			echo -e "[INFO] >>> ${dst_file}"
			cp ${src_file} ${dst_file}
		fi
	done

	umount ${mnt}
}

function l4t_bsp_fix()
{
<_L4T_BSP_FIX_CMD_>
}

######################################

if [[ $# -ge 1 ]]; then
	L4T_ROOTFS_RAW_IMG=$1
fi

if [[ ! -f ${L4T_ROOTFS_RAW_IMG} ]]; then
	type simg2img &> /dev/null
	if [[ $? -ne 0 ]]; then
		echo -e "\n[ERROR] \"simg2img\" command not found, please install with: \"sudo apt install android-sdk-libsparse-utils\".\n"
		exit 1
	fi
	simg2img ${L4T_ROOTFS_IMG} ${L4T_ROOTFS_RAW_IMG}
	if [[ $? -ne 0 ]]; then
		echo -e "\n[ERROR] Failed to generate \"system.img.raw\" file.\n"
		exit 1
	fi
fi

install_kernel_modules ${L4T_ROOTFS_RAW_IMG} $(get_kernel_vermagic ${L4T_KERNEL_IMG})

check_root

check_folder

check_bsp_st

l4t_bsp_fix

install_bsp_files resources

set_bsp_st

sync

echo -e "\n[INFO] Ritrontek L4T BSP installed successfully."
echo -e "Please run with following command to flash the target device:"
echo -e ">>> <_L4T_FLASH_CMD_>\n"
